/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.unindra.raport.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import edu.unindra.raport.dao.entity.Nilai;
import edu.unindra.raport.dao.interf.NilaiInterf;
import edu.unindra.raport.dao.koneksi.DatabaseUtilities;

/**
 * 
 * @author fendi
 */
public class NilaiImpl implements NilaiInterf {

	@Override
	public int insert(Nilai ni) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"insert into nilai values (?,?,?,?,?,?,?,?,?)");

			ps.setString(1, ni.getNis());
			ps.setString(2, ni.getNip());
			ps.setString(3, ni.getThn_ajaran());
			ps.setString(4, ni.getKls());
			ps.setString(5, ni.getTgs());
			ps.setString(6, ni.getUlhar());
			ps.setString(7, ni.getUts());
			ps.setString(8, ni.getUas());
			ps.setString(9, ni.getMat_pel());
			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}

	@Override
	public Nilai getNilai() throws SQLException {
		Nilai ni = new Nilai();

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from nilai");
			while (rs.next()) {
				ni.setNis(rs.getString("NIS"));
				ni.setNip(rs.getString("NIP"));
				ni.setThn_ajaran(rs.getString("TAHUN_AJARAN"));
				ni.setKls(rs.getString("KELAS"));
				ni.setTgs(rs.getString("NILAI_TUGAS"));
				ni.setUlhar(rs.getString("NILAI_ULHAR"));
				ni.setUts(rs.getString("NILAI_UTS"));
				ni.setUas(rs.getString("NILAI_UAS"));
				ni.setMat_pel(rs.getString("MATA_PELAJARAN"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return ni;
	}

	@Override
	public List<Nilai> getNilaiAll() throws SQLException {
		List<Nilai> sis = new LinkedList<Nilai>();
		Nilai ni = null;

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from nilai");
			while (rs.next()) {
				ni = new Nilai();

				ni.setNis(rs.getString("NIS"));
				ni.setNip(rs.getString("NIP"));
				ni.setThn_ajaran(rs.getString("TAHUN_AJARAN"));
				ni.setKls(rs.getString("KELAS"));
				ni.setTgs(rs.getString("NILAI_TUGAS"));
				ni.setUlhar(rs.getString("NILAI_ULHAR"));
				ni.setUts(rs.getString("NILAI_UTS"));
				ni.setUas(rs.getString("NILAI_UAS"));
				ni.setMat_pel(rs.getString("MATA_PELAJARAN"));

				sis.add(ni);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return sis;
	}

	@Override
	public int delete(String nis, String nip, String thn_ajaran, String kls)
			throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"delete from nilai where NIS = ? AND NIP = ? AND TAHUN_AJARAN = ? AND  KELAS = ? ");

			ps.setString(1, nis);
			ps.setString(2, nip);
			ps.setString(3, thn_ajaran);
			ps.setString(4, kls);
			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

		return result;
	}

	@Override
	public int update(Nilai ni) throws SQLException {
		int result = -1;

		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"update nilai set NILAI_TUGAS = ?,NILAI_ULHAR = ?,NILAI_UTS = ?,NILAI_UAS = ?  where NIS = ? AND NIP = ? AND TAHUN_AJARAN = ? AND  KELAS = ?");

			ps.setString(1, ni.getTgs());
			ps.setString(2, ni.getUlhar());
			ps.setString(3, ni.getUts());
			ps.setString(4, ni.getUas());
			ps.setString(5, ni.getNis());
			ps.setString(6, ni.getNip());
			ps.setString(7, ni.getThn_ajaran());
			ps.setString(8, ni.getKls());

			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}
}
